//
//  UUBarChart.m
//  UUChartDemo
//
//  Created by shake on 14-7-24.
//  Copyright (c) 2014年 uyiuyao. All rights reserved.
//

#import "UUBarChart.h"
#import "UUChartLabel.h"
#import "UUBar.h"

@interface UUBarChart ()
{
    UIScrollView *myScrollView;
    UUBar *newBar;//记录上一个bar
}
@end

@implementation UUBarChart {
    NSHashTable *_chartLabelsForX;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.clipsToBounds = YES;
        myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 0, frame.size.width - 20, frame.size.height)];
        myScrollView.backgroundColor = [UIColor clearColor];
        [self addSubview:myScrollView];
    }
    return self;
}

-(void)setYValues:(NSArray *)yValues
{
    _yValues = yValues;
    [self setYLabels:yValues];
}

-(void)setXLabels:(NSArray *)xLabels
{
    if( !_chartLabelsForX ){
        _chartLabelsForX = [NSHashTable weakObjectsHashTable];
    }
    
    _xLabels = xLabels;
    NSInteger num;
    if (xLabels.count>=8) {
        num = 8;
    }else if (xLabels.count<=4){
        num = 4;
    }else{
        num = xLabels.count;
    }
    
    for (int i=0; i<xLabels.count; i++) {
        UUChartLabel * label = [[UUChartLabel alloc] initWithFrame:CGRectMake((i *  _xLabelWidth ), self.frame.size.height - UULabelHeight, _xLabelWidth, UULabelHeight)];
        label.text = xLabels[i];
        
        [_chartLabelsForX addObject:label];
    }
    
    float max = (([xLabels count]-1)*_xLabelWidth + chartMargin)+_xLabelWidth;
    if (myScrollView.frame.size.width < max-10) {
        myScrollView.contentSize = CGSizeMake(max, self.frame.size.height);
    }
}

-(void)setColors:(NSArray *)colors
{
	_colors = colors;
}

- (void)setChooseRange:(CGRange)chooseRange
{
    _chooseRange = chooseRange;
}

-(void)strokeChart
{
    
    CGFloat chartCavanHeight = self.frame.size.height - UULabelHeight*3;
    for (int i=0; i<_yValues.count; i++)
    {
        float grade;//每一个状态的高度百分比
        UIColor *color;
        _sleepStatusModel = _yValues[i];
        if ([_sleepStatusModel isKindOfClass:[WXSDeepSleepStatusModel class]])//深睡眠
        {
            grade = 0.6;
            color = [UIColor colorWithRed:34/255.0 green:53/255.0 blue:251/255.0 alpha:1];
        }
        else if ([_sleepStatusModel isKindOfClass:[WXSShallowSleepStatusModel class]])//浅睡眠
        {
            grade = 1.0;
            color = [UIColor colorWithRed:88/255.0 green:229/255.0 blue:253/255.0 alpha:1];
        }
        else//醒着
        {
            grade = 0.5;
            color = [UIColor colorWithRed:253/255.0 green:180/255.0 blue:84/255.0 alpha:1];
        }
        _xLabelWidth = _sleepStatusModel.statusDuration / 300 * kScreenWidth;
        UUBar * bar = [[UUBar alloc] initWithFrame:CGRectMake(newBar ? (newBar.frame.origin.x + newBar.frame.size.width) : 0 + i ? _xLabelWidth : 0, UULabelHeight, _xLabelWidth , chartCavanHeight)];
        bar.backgroundColor = [UIColor clearColor];
            newBar = bar;
        bar.barColor = color;
        bar.gradePercent = grade;
        [myScrollView addSubview:bar];
    }
}

- (NSArray *)chartLabelsForX
{
    return [_chartLabelsForX allObjects];
}

@end
